package com.devops.admin.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.aliyuncs.AcsRequest;
import com.aliyuncs.AcsResponse;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.alidns.model.v20150109.*;
import com.devops.admin.model.DOSDomainRecord;
import com.devops.admin.model.Pagination;
import com.devops.admin.model.TableResult;
import com.devops.admin.service.AliyunReqResServer;
import com.devops.admin.vo.*;
import com.devops.shiro.vm.BaseUserVM;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class AliyunReqResServerImpl extends ApplicationObjectSupport implements AliyunReqResServer {

    @Override
    public <T extends AcsResponse> T getAcsResponse(AcsRequest<T> request, String accountType) {
        BaseUserVM user = (BaseUserVM) SecurityUtils.getSubject().getPrincipal();
        Assert.isTrue(null!=user,"该用户未认证");
        if (request instanceof AddDomainRecordRequest
                || request instanceof DeleteDomainRecordRequest
                || request instanceof UpdateDomainRecordRequest){
            Assert.isTrue(user.getAdminRole(),"权限不足");
        }

        try {
            IAcsClient acsClient = (IAcsClient)getApplicationContext().getBean(accountType + "AcsClient");
            return acsClient.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public AddDomainRecordResponse addDomainRecord(DOSDomainRecord request) {
        AddDomainRecordRequest request1 = new AddDomainRecordRequest();
        BeanUtil.copyProperties(request,request1);

        request1.setRR(request.getRr());
        request1.setTTL(request.getTtl());
        AddDomainRecordResponse acsResponse = getAcsResponse(request1,request.getAccountType());
        return acsResponse;
    }

    @Override
    public DeleteDomainRecordResponse deleteDomainRecord(DeleteDomainRecordRequestVo requestVo) {

        DeleteDomainRecordRequest request = new DeleteDomainRecordRequestVo();
        BeanUtil.copyProperties(requestVo,request);
        DeleteDomainRecordResponse acsResponse = getAcsResponse(request, requestVo.getAccountType());
        return acsResponse;
    }

    @Override
    public UpdateDomainRecordResponse updateDomainRecord(DOSDomainRecord request) {
        UpdateDomainRecordRequest request1 = new UpdateDomainRecordRequest();
        BeanUtil.copyProperties(request,request1);

        request1.setRR(request.getRr());
        request1.setTTL(request.getTtl());
        UpdateDomainRecordResponse acsResponse = getAcsResponse(request1,request.getAccountType());
        return acsResponse;
    }

    @Override
    public TableResult<DescribeDomainsResponse.Domain> getDomains(DescribeDomainsRequestVo requestVo, DOSPageVo pageVo) {
        DescribeDomainsRequest request = new DescribeDomainsRequestVo();
        BeanUtil.copyProperties(requestVo,request);
        //返回结果初始化
        TableResult<DescribeDomainsResponse.Domain> tableResult = new TableResult<>();
        //处理分页数据
        request.setPageNumber(Long.valueOf(pageVo.getPageNum()));
        request.setPageSize(Long.valueOf(pageVo.getPageSize()));
        //通过阿里云SDK获取数据
        DescribeDomainsResponse acsResponse = getAcsResponse(request,requestVo.getAccountType());
        if (null == acsResponse){
            return tableResult;
        }
        Pagination p = Pagination.builder().pageNum(acsResponse.getPageNumber())
                .pageSize(acsResponse.getPageSize())
                .total(acsResponse.getTotalCount())
                .build();
        tableResult.setList(acsResponse.getDomains());
        tableResult.setPagination(p);
        return tableResult;
    }

    @Override
    public DescribeDomainInfoResponse getDomainInfo(DescribeDomainInfoRequestVo requestVo) {
        DescribeDomainInfoRequest request = new DescribeDomainInfoRequestVo();
        BeanUtil.copyProperties(requestVo,request);
        DescribeDomainInfoResponse acsResponse = getAcsResponse(request,requestVo.getAccountType());
        return acsResponse;
    }

    @Override
    public TableResult<DescribeDomainLogsResponse.DomainLog> getDomainOperateLogs(DescribeDomainLogsRequestVo requestVo, DOSPageVo pageVo) {
        DescribeDomainLogsRequest request = new DescribeDomainLogsRequest();
        BeanUtil.copyProperties(requestVo,request);
        //返回数据初始化
        TableResult<DescribeDomainLogsResponse.DomainLog> tableResult = new TableResult<>();
        //分页设置
        request.setPageNumber(Long.valueOf(pageVo.getPageNum()));
        request.setPageSize(Long.valueOf(pageVo.getPageSize()));
        //通过阿里云SDK获取数据
        DescribeDomainLogsResponse acsResponse = getAcsResponse(request, requestVo.getAccountType());
        if (null == acsResponse){
            return tableResult;
        }
        Pagination p = Pagination.builder().pageNum(acsResponse.getPageNumber())
                .pageSize(acsResponse.getPageSize())
                .total(acsResponse.getTotalCount())
                .build();
        tableResult.setList(acsResponse.getDomainLogs());
        tableResult.setPagination(p);
        return tableResult;
    }

    @Override
    public TableResult<DescribeDomainRecordsResponse.Record> getDomainRecords(DOSDescribeDomainRecordsRequestVo requestVo, DOSPageVo pageVo) {
        //返回结果初始化
        TableResult<DescribeDomainRecordsResponse.Record> tableResult = new TableResult<>();
        DescribeDomainRecordsRequest recordsRequest = new DescribeDomainRecordsRequest();
        BeanUtil.copyProperties(requestVo,recordsRequest);
        recordsRequest.setRRKeyWord(requestVo.getRrKeyWord());
        //获取分页配置
        recordsRequest.setPageNumber(Long.valueOf(pageVo.getPageNum()));
        recordsRequest.setPageSize(Long.valueOf(pageVo.getPageSize()));
        //调用阿里云SDK获取数据
        DescribeDomainRecordsResponse acsResponse = getAcsResponse(recordsRequest,requestVo.getAccountType());
        if (acsResponse == null) {
            return tableResult;
        }
        Pagination p = Pagination.builder().pageNum(acsResponse.getPageNumber())
                .pageSize(acsResponse.getPageSize())
                .total(acsResponse.getTotalCount())
                .build();
        tableResult.setPagination(p);
        tableResult.setList(acsResponse.getDomainRecords());
        return tableResult;
    }

    @Override
    public DescribeDomainRecordInfoResponse getDomainRecordInfo(DescribeDomainRecordInfoRequestVo requestVo) {
        DescribeDomainRecordInfoRequest request = new DescribeDomainRecordInfoRequestVo();
        BeanUtil.copyProperties(requestVo,request);
        DescribeDomainRecordInfoResponse acsResponse = getAcsResponse(request, requestVo.getAccountType());
        return acsResponse;
    }
}
